package owen.struts.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.UnavailableException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;

import owen.utils.FileSearch;

public class OwenJavaActionServlet extends ActionServlet {

	private static final long serialVersionUID = 1L;

	public void init() throws ServletException {
		try {
			initInternal();
			initOther();
			initServlet();
			getServletContext().setAttribute(
					"org.apache.struts.action.ACTION_SERVLET", this);
			initModuleConfigFactory();

			ModuleConfig moduleConfig = initModuleConfig("", this.config);
			initModuleMessageResources(moduleConfig);
			initModuleDataSources(moduleConfig);
			initModulePlugIns(moduleConfig);
			moduleConfig.freeze();
			String path = getServletContext().getRealPath("WEB-INF");
			FileSearch obj = new FileSearch();
			StringBuffer sb = new StringBuffer();
			String start = "struts-config-";
			String end = ".xml";
			obj.getFilePath(sb, path, start, end);
			String[] filePath = sb.toString().split(";");
			String tmp = "";
			Map map = new HashMap();
			for (int i = 0; i < filePath.length; i++) {
				if (filePath[i] == null || filePath[i].equals(""))
					continue;
				String tmpPath = "\\WEB-INF" + filePath[i].replace(path, "");
				String prefix = tmpPath.substring(tmpPath.indexOf(start),
						tmpPath.indexOf(end));
				prefix = "/" + prefix.replace(start, "");
				tmpPath = tmpPath.replace("\\", "/");
				
				//System.out.println("prefix:"+prefix);
				//System.out.println("prefix2:"+prefix.replace(start, ""));
				//System.out.println("tmppath:"+tmpPath);
				if (map.get(prefix) != null) {
					tmp = String.valueOf(map.get(prefix));
					map.put(prefix, tmp + "," + tmpPath);
				} else {
					map.put(prefix, tmpPath);
				}
			}
			Entry entry = null;
			Iterator iter = map.entrySet().iterator();
			while (iter.hasNext()) {
				entry = (Entry) iter.next();
				log.info("load xml file " + entry.getValue().toString());
				moduleConfig = initModuleConfig(String.valueOf(entry.getKey()),
						String.valueOf(entry.getValue()));
				initModuleMessageResources(moduleConfig);
				initModuleDataSources(moduleConfig);
				initModulePlugIns(moduleConfig);
				moduleConfig.freeze();
			}

			initModulePrefixes(getServletContext());

			destroyConfigDigester();
		} catch (UnavailableException ex) {
			throw ex;
		} catch (Throwable t) {
			t.printStackTrace();
			log.error("Unable to initialize Struts "
					+ "ActionServlet due to an "
					+ "unexpected exception or error " + "thrown, so marking "
					+ "the servlet as unavailable.  "
					+ "Most likely, this is due to an "
					+ "incorrect or missing library dependency.", t);
			throw new UnavailableException(t.getMessage());
		}
	}
}
